<style scoped>
    .layout{
        border: 1px solid #d7dde4;
        background: #f5f7f9;
        position: relative;
        border-radius: 4px;
        overflow: hidden;
    }
    .layout-breadcrumb{
        padding: 10px 15px 0;
    }
    .layout-content{
        min-height: 500px;
        margin: 15px;
        overflow: hidden;
        background: #fff;
        border-radius: 4px;
    }
    .layout-content-main{
        padding: 10px;
    }
    .layout-copy{
        text-align: center;
        padding: 10px 0 20px;
        color: #9ea7b4;
    }
    .layout-menu-left{
        background: #464c5b;
    }
    .layout-header{
        height: 60px;
        background: #464c5b;
        box-shadow: 0 1px 1px rgba(0,0,0,.1);
    }
    .layout-logo-left{
        width: 90%;
        height: 30px;
        background: #5b6270;
        border-radius: 3px;
        margin: 15px auto;
    }
    .layout-ceiling-main a{
        color: #9ba7b5;
    }
    .layout-hide-text .layout-text{
        display: none;
    }
    .ivu-col{
        transition: width .2s ease-in-out;
    }
    .title{
        float:left;
        font-size:38px;
        line-height:60px;
        color:#fff;
       
    }
    .myform{
        position:absolute;
        width:300px;
        height:200px;
        border:1px solid #ccc;
        left:50%;
        margin-left:-150px;
        margin-top:-170px;
        top:50%;
        text-align:center;
        border-radius:10px;
    }

</style>
<template>
    <div>
        <div v-if="login">
            <div class="layout"  :class="{'layout-hide-text': spanLeft < 5}">
                <Row type="flex">
                    <Col :span="spanLeft" class="layout-menu-left">
                        <Menu active-name="1" theme="dark" width="auto" @on-select="getSelect">
                            <div class="layout-logo-left"></div>
                            <MenuItem name="首页">
                                <Icon type="ios-home" :size="iconSize"></Icon>
                                <span class="layout-text">
                                <router-link to='/'>首页</router-link>
                                </span>
                            </MenuItem>
                            <MenuItem name="用户管理">
                                <Icon type="ios-people" :size="iconSize"></Icon>
                                <span class="layout-text">
                                <router-link to='/user'>用户管理</router-link>
                                </span>
                            </MenuItem>
                            <MenuItem name="分类管理">
                                <Icon type="android-locate" :size="iconSize"></Icon>
                                <span class="layout-text">
                                <router-link to='/calssify'>分类管理</router-link>
                                </span>
                            </MenuItem>
                            <MenuItem name="新闻管理">
                                <Icon type="document-text" :size="iconSize"></Icon>
                                <span class="layout-text">
                                <router-link to='/news'>新闻管理</router-link>
                                </span>
                            </MenuItem>
                            <MenuItem name="上传管理">
                                <Icon type="arrow-up-a" :size="iconSize"></Icon>
                                <span class="layout-text">
                                <router-link to='/upload'>上传管理</router-link>
                                </span>
                            </MenuItem>
                            <MenuItem name="产品管理">
                                <Icon type="soup-can" :size="iconSize"></Icon>
                                <span class="layout-text">
                                <router-link to='/'>产品管理</router-link>
                                </span>
                            </MenuItem>
                        </Menu>
                    </Col>
                    <Col :span="spanRight">
                        <div class="layout-header">
                            <Button type="text" @click="toggleClick" style="float:left">
                                <Icon type="navicon" size="32" style='color:#fff'></Icon>
                            </Button>
                            <div class="title">XXX后台管理系统</div>
                        </div>
                        <div class="layout-breadcrumb">
                            <Breadcrumb>

                                <BreadcrumbItem :href="menuPath">{{selectName}}</BreadcrumbItem>
                            </Breadcrumb>
                        </div>
                        <div class="layout-content">
                            <div class="layout-content-main">
                            <router-view></router-view>
                            </div>
                        </div>
                        <div class="layout-copy">
                            2011-2016 &copy; 范秀芳
                        </div>
                    </Col>
                </Row>
            </div>
        </div>
        <div v-else>
            <Form ref="formInline" :model="formInline" :rules="ruleInline" inline class="myform">
                <FormItem prop="name" style="margin-top:50px;">
                    <Input type="text" v-model="formInline.name" placeholder="Username">
                        <Icon type="ios-person-outline" slot="prepend"></Icon>
                    </Input>
                </FormItem>
                <br/>
                <FormItem prop="password">
                    <Input type="password" v-model="formInline.password" placeholder="Password">
                        <Icon type="ios-locked-outline" slot="prepend"></Icon>
                    </Input>
                </FormItem>
                <br/>
                <FormItem>
                    <Button type="primary" @click="handleSubmit('formInline')" style="margin-bottom:50px;">登录</Button>
                </FormItem>
            </Form>
        </div>
    </div>
</template>
<script>
    import {mapGetters,mapActions} from 'vuex';
    import md5 from 'crypto-js/sha256';
    export default {
        data () {
            return {
                spanLeft: 5,
                spanRight: 19,
                menuPath:'/',
                selectName:'首页',
                formInline: {
                    name: '',
                    password: ''
                },
                ruleInline: {
                    name: [
                        { required: true, message: '请填写用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请填写密码', trigger: 'blur' },
                        { type: 'string', min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
                    ]
                }
            }
        },
        computed: {
            ...mapGetters(['login']),
            iconSize () {
                return this.spanLeft === 5 ? 14 : 24;
            },
        },
        methods: {
            ...mapActions(['checkLogin']),
            toggleClick () {
                if (this.spanLeft === 5) {
                    this.spanLeft = 2;
                    this.spanRight = 22;
                } else {
                    this.spanLeft = 5;
                    this.spanRight = 19;
                }
            },
            getSelect(name){
                this.menuPath=this.$route.path;
                this.selectName=name
            },
            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.formInline.password=md5(this.formInline.password).toString();
                        this.$http.post("http://localhost:3000/users/check",this.formInline)
                        .then((res)=>{
                            if(res.data.length>0){
                                this.checkLogin(true);
                                localStorage.login=true;
                                this.$Message.success('登录成功!');
                            }else{
                                this.$Message.error('用户名或密码错误!');
                            }
                        })
                    } else {
                        this.$Message.error('表单验证失败!');
                        this.formInline={};
                    }
                })
            }
        },
        created(){
            this.checkLogin(localStorage.login);
        }
    }
</script>